<template>
  <div class="register">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="title">注册</div>
      </div>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item prop="pass">
          <el-input
            type="text"
            v-model="ruleForm.userName"
            prefix-icon="el-icon-user"
            autocomplete="off"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="checkPass">
          <el-input
            type="password"
            v-model="ruleForm.Pass"
            prefix-icon="el-icon-lock"
            autocomplete="off"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="age">
          <el-input
            v-model="ruleForm.email"
            prefix-icon="el-icon-message"
            placeholder="请输入邮箱"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm()">注册</el-button>
          <div class="re">
            已有账号？
            <router-link to="/login">
              <el-link type="primary">点击登陆</el-link>
            </router-link>
          </div>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import request from '../../utils/request'

export default {
  created () {

  },
  data () {
    const checkEmail = (rule, value, callback) => {
      if (!value) callback(new Error('邮箱不能为空'))
    }
    const validateAccount = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('账号不能为空'))
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('密码不能为空'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        userName: '',
        Pass: '',
        email: ''
      },
      rules: {
        userName: [{ validator: validateAccount, trigger: 'blur' }],
        Pass: [{ validator: validatePass, trigger: 'blur' }],
        email: [{ validator: checkEmail, trigger: 'blur' }]
      }
    }
  },
  methods: {
    submitForm () {
      console.log(this.ruleForm)

      request.get('register/', {
        params: {
          account: this.ruleForm.userName,
          password: this.ruleForm.Pass,
          usermail: this.ruleForm.email
        }
      })
        .then(res => {
          console.log(res)
          // const { data: { statusCode } } = code
          this.$message.success('注册成功')
          this.$router.push('/login')
        })
        .catch(error => {
          console.log(error)
        })
    }
  }
}
</script>

<style scope>
body {
  background: url("../assets/bg/1.jpeg");
  background-size: 100% 100%;
}
.is-always-shadow {
  margin: auto;
  margin-top: 400px;
}
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}

.box-card {
  width: 350px;
  height: 400px;
}
.title {
  margin-top: 20px;
}
.el-input {
  width: 310px;
}
.el-form-item__content {
  margin-left: 0px !important;
}
.el-button {
  width: 310px;
}
.re {
  margin-left: 85px;
  margin-top: 10px;
}
</style>
